{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44a281e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_breast_cancer\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "dataset = load_breast_cancer()\n",
    "x,y =dataset.data,dataset.target\n",
    "print(x,shape)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe3d3337",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "x = StandardScaler().fit_transform(x)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f40db66a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=420)\n",
    "for kernel in kernels:\n",
    "    model =SVC(kernel=kernel,gamma=\"auto\",degree=1)\n",
    "    pred = model.predict(x_test)\n",
    "    ac = accuracy_score(y_test,pred)\n",
    "    print(f'选择{kernel}函数时，准确率为：{ac}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0a4a135",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import StratifiedShuffleSplit\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "gamma_range=np.logspace(-10,1,20)\n",
    "coef0_range=np.linspace(0,5,10)\n",
    "param_grid=dict(gamma=gamma_range,coef0=coef0_range)\n",
    "cv=StratifiedShuffleSplit(n_splits=5,test_size=0.3,random_state=420)\t\t       \n",
    "grid=GridSearchCV(SVC(kernel=\"poly\",degree=1),param_grid=param_grid,cv=cv)\t       \n",
    "grid.fit(x,y)\n",
    "print(\"最优参数值为：%s\"%grid.best_params_)\n",
    "print(\"选取该参数值时，模型的预测准确率为：%f\"%grid.best_score_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bb7b6fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "import matplotlib.pyplot as plt\n",
    "score=[]\n",
    "C_range=np.linspace(0.01,30,50)\n",
    "for i in C_range:\n",
    "    model=SVC(kernel='rgb',C=i)\n",
    "    model.fit(x_train,y_train)\n",
    "    pred=model.predict(x_test)\n",
    "    ac=accuracy_score(y_test,pred)\n",
    "    score.append(ac)\n",
    "plt.plot(C_range,score)\n",
    "plt.show()\n",
    "print(\"模型的最优C值为：%s\"%C_range[score.index(max(score))])\n",
    "print(\"模型选取该参数时的预测准确率为：%f\"%max(score))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f7ad1a7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
